package com.oolatina.adapters;

import java.util.ArrayList;
import java.util.HashMap;

import newspackage.NewsBean;

import cc.ImageLoader2;

import com.oolatina.R;
import com.oolatina.activity.MainActivity;
import com.oolatina.views.RemoteImageView;

import android.app.Activity;
import android.content.Context;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class NewsAdapter extends BaseAdapter {
	private Context mContext;
	private LayoutInflater inflater;
	private ViewHolder holder;
	private String[] mLabels;
	private ArrayList<NewsBean> mData;
	ImageLoader2 imageLoader = null;
	Activity activity;

	public NewsAdapter(Context c, ArrayList<NewsBean> data, Activity a) {
		mContext = c;
		mData = data;
		activity = a;

		try {
			inflater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			imageLoader = new ImageLoader2(mContext.getApplicationContext());
		} catch (Exception e) {
		}
	}

	public int getCount() {
		return mData.size();
	}

	public Object getItem(int position) {
		return null;
	}

	public long getItemId(int position) {
		return 0;
	}

	static class ViewHolder {
		public RelativeLayout mainRl;
		public ImageView image;
		public TextView newsName, newsDetail;
	}

	// create a new ImageView for each item referenced by the Adapter
	public View getView(final int position, View convertView, ViewGroup parent) {
		ImageView imageView;
		if (convertView == null) { // if it's not recycled, initialize some
									// attributes
			convertView = inflater.inflate(R.layout.news_view, null);
			holder = new ViewHolder();
			holder.image = (ImageView) convertView.findViewById(R.id.NewsImage);
			holder.mainRl = (RelativeLayout) convertView
					.findViewById(R.id.mainRL);
			holder.newsName = (TextView) convertView
					.findViewById(R.id.NewsName);
			holder.newsDetail = (TextView) convertView
					.findViewById(R.id.NewsDetail);
			holder.newsDetail.setMovementMethod(new ScrollingMovementMethod());

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (mData.get(position).getImage().length() > 10) {
			imageLoader.DisplayImage(mData.get(position).getImage(),
					holder.image);
		}
		holder.newsName.setText(mData.get(position).getTitle());

		String newsDetailString = mData.get(position).getDescription();

		if (newsDetailString.length() > 100) {

			newsDetailString = newsDetailString.substring(0,
					newsDetailString.length() - 1);
			holder.newsDetail.setText(newsDetailString + "...");
		} else {
			newsDetailString = newsDetailString.substring(0, 100);
			holder.newsDetail.setText(newsDetailString + "...");
		}

		final MainActivity sct = (MainActivity) activity;

		holder.mainRl.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				String newsTitle = mData.get(position).getTitle();
				String newsDesc = mData.get(position).getDescription();

				String imageUrl = mData.get(position).getImage();

				sct.callNewsDetail(newsTitle, newsDesc, imageUrl);
				// Toast.makeText(mContext, "News Detail", Toast.LENGTH_LONG)
				// .show();

			}
		});

		holder.newsDetail.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				String newsTitle = mData.get(position).getTitle();
				String newsDesc = mData.get(position).getDescription();

				String imageUrl = mData.get(position).getImage();

				sct.callNewsDetail(newsTitle, newsDesc, imageUrl);
				// Toast.makeText(mContext, "News Detail", Toast.LENGTH_LONG)
				// .show();

			}
		});

		return convertView;
	}
}
